AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

ENT.WireDebugName = "Lagmeter"

function ENT:Initialize()
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )

	self.Inputs = WireLib.CreateSpecialInputs(self.Entity, {"Player"}, {"ENTITY"})    //Set up inputs
	self.Outputs = Wire_CreateOutputs(self.Entity, {"Ping", "FPS", "Loss", "Usmg/s"}) //Set up outputs
end

function ENT:Setup()
	self.Value = 0
	self.Player = NullEntity()

	Wire_TriggerOutput(self.Entity, "Ping", 0)
	Wire_TriggerOutput(self.Entity, "FPS", 0)
	Wire_TriggerOutput(self.Entity, "Loss", 0)
	Wire_TriggerOutput(self.Entity, "Usmg/s", 0)
end

function ENT:Think()
	self.BaseClass.Think(self)
	
	if (self.Player ~= NullEntity() && self.Player:IsPlayer() && self.Player:IsValid()) then
		Wire_TriggerOutput(self.Entity, "Ping", self.Player:Ping())
		Wire_TriggerOutput(self.Entity, "Loss", self.Player:PacketLoss())
	else
		Wire_TriggerOutput(self.Entity, "Ping", 0)
		Wire_TriggerOutput(self.Entity, "Loss", 0)
	end
	
	self.Entity:NextThink(CurTime()+0.04)
	return true
end

function ENT:TriggerInput(iname, value)
	if (iname == "Player") then
		if (value:IsPlayer() && value:IsValid()) then
			self.Player = value
			Wire_TriggerOutput(self.Entity, "Ping", value:Ping())
			Wire_TriggerOutput(self.Entity, "Loss", value:PacketLoss())
		end
	end
end
